var file = "contest.php";
var contest_id = 0;
var bModified = false;

function InitTest(Id)
{
	contest_id = Id;
	var area = document.getElementById("src_area");
	area.style.height = (document.body.offsetHeight - 100) + "px";
	
	EmptyElement("src_area");
	EmptyElement("contest");
	GetPageContent("GET", "test.php?id=" + Id + "&do=src&file=" + file, "", FillElementValue, "src_area", true);
	GetPageContent("GET", "test.php?id=" + Id + "&do=exec_contest", "", FillElement, "contest", true);
	var list = document.getElementById("file");
	list.options.selectedIndex = 0;
}

function GetHTTPObject()
{
	var xhr; 
    try {  xhr = new ActiveXObject('Msxml2.XMLHTTP');   }
    catch (e) 
    {
        try {   xhr = new ActiveXObject('Microsoft.XMLHTTP');    }
        catch (e2) 
        {
          try {  xhr = new XMLHttpRequest();     }
          catch (e3) {  xhr = false;   }
        }
    }
	return xhr;
}

function GetPageContent(Type, Url, Params, Callback, cbParam, Sync)
{
	var xhr = GetHTTPObject();
	xhr.onreadystatechange = function()
	{
		if(xhr.readyState == 4 && Callback != null)
		{
			if(xhr.status == 200 || xhr.status == 0)
				Callback(true, xhr.responseText, cbParam);
			else
				CallBack(false, "Error code : " + xhr.status, cbParam);
		}
	};

	SetResult(true, "Loading " + Url + "... ", "request");
	xhr.open(Type, Url, Sync);
	if(Type == "POST")
	{
		xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		xhr.send(Params);
	}
	else
		xhr.send(null);
}

function EmptyElement(id)
{
	var e = document.getElementById(id);
	e.innerHTML = '';
	e.value = '';
}

function FillElement(Success, Text, Element)
{
	document.getElementById(Element).innerHTML = Text;
	SetResult(Success, (Success?"OK : élément mis à jour":"ERREUR : échec de la mise à jour"), "result");
}

function FillElementValue(Success, Text, Element)
{
	document.getElementById(Element).value = Text;
	SetResult(Success, (Success?"OK : élément mis à jour":"ERREUR : échec de la mise à jour"), "result");
}

function SrcAreaChanged()
{
	var list = document.getElementById("file");
	list.options[list.selectedIndex].text = file + "*";
	bModified = true;
}

function SaveSrc()
{
	var src = document.getElementById("src_area").value;
	src = encodeURIComponent(src);
	GetPageContent("POST", "test.php?id=" + contest_id + "&do=save", "file=" + file + "&src=" + src, CheckResult, "", false);
}

function UpdateContest()
{
	EmptyElement("contest");
	GetPageContent('GET', 'test.php?id=' + contest_id + '&do=exec_contest', '', FillElement, 'contest', true);
}

function SaveAndUpdate()
{
	SaveSrc();
	UpdateContest();
}

function CheckResult(Success, Text, Param)
{
	if(Text.charAt(0) == 'O' && Text.charAt(1) == 'K')
	{
		ResetFileList();
		bModified = false;
		SetResult(true, Text, "result");
	}
	else
		SetResult(false, Text, "result");	
}

function ResetFileList()
{
	var list = document.getElementById("file");
	list.options[0].text = "contest.php";
	list.options[1].text = "validation.php";
}

function SetResult(Success, Text, Element)
{
	var e = document.getElementById(Element);
	e.innerHTML = Text;
	if(Success) e.style.color = "green";
	else		e.style.color = "red";
}

function Reload()
{
	EmptyElement("src_area");
	GetPageContent('GET', 'test.php?id=' + contest_id + '&do=src&file=' + file, '', FillElementValue, 'src_area', true);
}

function ChangeFile()
{
	if(bModified)
	{
		if(confirm("Le fichier à été modifié.\nVoulez-vous l'enregistrer ?"))
		{
			SaveSrc();
		}
		else
		{
			bModified = false;
			ResetFileList();
		}
	}
	
	var list = document.getElementById("file");
	switch(list.options.selectedIndex)
	{
	case 0: file = "contest.php"; break;
	case 1: file = "validation.php"; break;
	}
	EmptyElement("src_area");
	GetPageContent('GET', 'test.php?id=' + contest_id + '&do=src&file=' + file, '', FillElementValue, 'src_area', true);
}

function RequestTest()
{
	var vars = GetVars();
	EmptyElement("validation");
	GetPageContent('POST', 'test.php?id=' + contest_id + '&do=test', vars, FillElement, "validation", true);
}

function AddVar()
{
	var e = document.getElementById("vars");
	var html = '<div style="clear:both;"><input style="width:150px;height:15px;float:left;"></input><textarea style="width:' + (e.offsetWidth - 150 - 60) + 'px;height:15px;margin:0;"></textarea><button onclick="RemoveVar(this);" style="margin:0;margin-right:5px;float:right;padding:0;width:20px;">X</button></div>';
	if(e.insertAdjacentHTML)
	{	
		e.insertAdjacentHTML("beforeEnd", html);
	}
	else
	{
		var range = document.createRange();
	    var frag = range.createContextualFragment(html);
		e.appendChild(frag);
	}
}

function RemoveVar(e)
{
	e.parentNode.parentNode.removeChild(e.parentNode);
}

function GetVars()
{
	var div = document.getElementById("vars"); 
	var vars_name = div.getElementsByTagName("input");
	var vars_content = div.getElementsByTagName("textarea");
	var post = "";
	var min = (vars_name.length>vars_content.length?vars_name.length:vars_content.length);
	for(var i = 0; i < min; i++)
	{
		post += vars_name[i].value + '=' + vars_content[i].value;
		if(i != min - 1)
			post += "&";
    }
	return post;
}